package cn.hx.whh.automaticticket.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import com.supermap.filetransfer.execute.Tasker;
import com.supermap.filetransfer.execute.download.DownloadTasker;
import com.supermap.filetransfer.listeners.IDownloadListener;
import com.supermap.filetransfer.thread.DownloadThread;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import cn.hx.whh.automaticticket.R;
import okhttp3.Call;

public class IndexActivity extends Activity {
    private Timer timer;
    private TimerTask task;
    private ImageView conduct_propaganda;
    private TextView bottom;

    private TextView btDownload;

    private DownloadTasker tasker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        conduct_propaganda = (ImageView) findViewById(R.id.conduct_propaganda);
        bottom = (TextView) findViewById(R.id.bottom);
        bottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // automaticPrint(view);
                if (tasker!=null && tasker.isNotStop == true  ){
                    tasker.onStop(0L);
                }else  if (tasker!=null && !tasker.isNotStop){
                    tasker.onContinue();
                }
                // tasker.onContinue();
            }
        });

        btDownload = (TextView) findViewById(R.id.bt_download);
        btDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tasker = DownloadTasker
                        .build()
                        .dirFile("/storage/emulated/0/SuperMap_Mobile_XiePo/screen_capture/")
                        .listener(listener)
                        .fileName(System.currentTimeMillis() + "")
                        .url("http://10.10.10.106:8080/download").create();
            }
        });

        // this.timer = new Timer(true);
        /*this.task = new TimerTask() {
            public void run() {
                switchActivity();
            }
        };*/
        //init();

        ActivityCompat.requestPermissions(IndexActivity.this,
                new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},
                1);
    }

    private void init() {
        timer.schedule(task, 1000 * 60);
    }

    private void rotationImage(String path) {

    }

    public void automaticPrint(View view) {
        // startActivity(new Intent(this, AutomaticTicketActivity.class));

    }

    private void switchActivity() {
        startActivity(new Intent(this, QrCodeIndexActivity.class));
    }

    private void closeTimer() {
        if (this.timer != null) {
            this.timer.cancel();
            this.timer = null;
        }
        if (this.task != null) {
            this.task.cancel();
            this.task = null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        closeTimer();
    }

    @Override
    public void onStop() {
        super.onStop();
        closeTimer();
    }

    private MyDownloadListener listener = new MyDownloadListener();

    private class MyDownloadListener implements IDownloadListener {

        @Override
        public void onInit(Tasker taks) {
            System.err.println("onInit");
        }

        @Override
        public void onStart(Tasker taks) {
            System.err.println("onStart");
        }

        @Override
        public boolean onStop(long msec) {
            return false;
        }

        @Override
        public boolean onCancel() {
            return false;
        }

        @Override
        public boolean onContinue() {
            return false;
        }

        @Override
        public void onFail(Call call, Exception e, Tasker tasker) {
            System.err.println("下载失败");
        }

        @Override
        public void onSubtaskFail(Call call, Exception e, DownloadThread tasker) {
            System.err.println("子任务下载失败");
        }

        @Override
        public void onSubtaskSuccess(DownloadThread tasker) {
            System.err.println("子任务下载完成");
        }

        @Override
        public void onComplete(Tasker tasker) {
            System.err.println("下载完成");
        }

        @Override
        public void onRunning(Tasker tasker) {
            System.err.println("下载进度:" + ((DownloadTasker) tasker).progressBar);
        }
    }
}
